/**
 * 
 */
package org.log4jconfig.xml.editor.quickfix;

import java.net.URL;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.jface.text.contentassist.IContextInformation;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.ui.IMarkerResolution2;
import org.eclipse.wst.sse.core.StructuredModelManager;
import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel;
import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument;
import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion;
import org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion;
import org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList;
import org.eclipse.wst.xml.core.internal.regions.DOMRegionContext;
import org.log4jconfig.xml.editor.Log4jConfigXmlEditorIntegrationPlugin;
import org.log4jconfig.xml.editor.utils.Logger;
import org.log4jconfig.xml.editor.utils.StructuredDocumentRegionUtils;

/**
 * @author Xi
 * 
 */
@SuppressWarnings("restriction")
public class AllDeprecatedElementsReplaceQuickFix implements IMarkerResolution2, ICompletionProposal {

	public static final String REPLACE_ALL_DEPRECATED_TAGS = "Replace *all* deprecated <category> and <priority> tags with <logger> and <level> tags, respectively.";

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.IMarkerResolution#getLabel()
	 */
	public String getLabel() {
		return AllDeprecatedElementsReplaceQuickFix.REPLACE_ALL_DEPRECATED_TAGS;
	}
	
	public String getDescription() {
		return AllDeprecatedElementsReplaceQuickFix.REPLACE_ALL_DEPRECATED_TAGS;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.jface.text.contentassist.ICompletionProposal#getDisplayString
	 * ()
	 */
	public String getDisplayString() {
		return AllDeprecatedElementsReplaceQuickFix.REPLACE_ALL_DEPRECATED_TAGS;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.ui.IMarkerResolution#run(org.eclipse.core.resources.IMarker)
	 */
	public void run(IMarker marker) {
		if (marker.getResource() instanceof IFile && marker.getResource().exists()) {
			IStructuredModel model = StructuredModelManager.getModelManager().getExistingModelForEdit(
					(IFile) marker.getResource());

			IStructuredDocument document = model.getStructuredDocument();
			replaceInDocument(document);

		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.jface.text.contentassist.ICompletionProposal#apply(org.eclipse
	 * .jface.text.IDocument)
	 */
	public void apply(IDocument document) {
		if (document instanceof IStructuredDocument) {
			replaceInDocument((IStructuredDocument) document);
		}

	}

	/**
	 * @param document
	 * @throws BadLocationException
	 */
	protected void replaceInDocument(IStructuredDocument document) {
		try {
			IStructuredDocumentRegion structuredRegion = document.getFirstStructuredDocumentRegion();
			while (structuredRegion != null) {
				String tagName = StructuredDocumentRegionUtils.extractTagName(structuredRegion);
				if ("category".equals(tagName) || "priority".equals(tagName)) {
					ITextRegionList regionsList = structuredRegion.getRegions();
					for (int i = 0; i < regionsList.size(); i++) {
						ITextRegion region = regionsList.get(i);
						if (DOMRegionContext.XML_TAG_NAME.equals(region.getType())) {
							if ("category".equals(tagName)) {
								document.replace(structuredRegion.getStart() + region.getStart(), tagName.length(),
										"logger");
							} else if ("priority".equals(tagName)) {
								document.replace(structuredRegion.getStart() + region.getStart(), tagName.length(),
										"level");
							}
						}
					}

				}
				// move (forward) to next document region
				structuredRegion = structuredRegion.getNext();
			}
		} catch (Exception e) {
			Logger.error("Error while running quickfix: " + e.getMessage(), e);
		}
	}

	public Image getImage() {
		URL url = Log4jConfigXmlEditorIntegrationPlugin.getDefault().getBundle().getResource(
				"icons/correction_change.gif");
		return ImageDescriptor.createFromURL(url).createImage();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @seeorg.eclipse.jface.text.contentassist.ICompletionProposal#
	 * getAdditionalProposalInfo()
	 */
	public String getAdditionalProposalInfo() {
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @seeorg.eclipse.jface.text.contentassist.ICompletionProposal#
	 * getContextInformation()
	 */
	public IContextInformation getContextInformation() {
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.jface.text.contentassist.ICompletionProposal#getSelection
	 * (org.eclipse.jface.text.IDocument)
	 */
	public Point getSelection(IDocument document) {
		/*
		 * CompletionProposal proposal = new CompletionProposal(
		 * getReplacementString(), getReplacementOffset(),
		 * getReplacementLength(), getCursorPosition(), getImage(),
		 * getDisplayString(), getContextInformation(), null); return
		 * proposal.getSelection(document);
		 */
		return null;
	}

}
